using System;
using System.IO;
using System.Text;
using System.Web;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0499 //2010.03.18 //Begin
namespace InfoJetSoft.Service.Worker
{
	class InkPicturesLoadWorker
	{
		public static string loadInkPictures( Form form )
		{
			StringBuilder buffer = new StringBuilder();
			string parent = "";
			if( ConfigUtil.UseAJAX() )
			{
				parent = "";
			}
			else
			{
				buffer.Append( "<script>" );
				parent = "parent.";
			}
			buffer.Append( parent + "InfoJet_StopProgress();" );
			buffer.Append( parent + "InfoJet_Postbacking = 0;" );
			buffer.Append( parent + "InfoJet_OnPostbackEnd();" );
			try
			{
				string formId = form.HttpContext.Request.Params[ "xdoc_param_form_id" ];
				string backgroundUrl = form.HttpContext.Request.Params[ "xdoc_param_background_picture_url" ];
				string inkUrl = form.HttpContext.Request.Params[ "xdoc_param_ink_picture_url" ];
				buffer.Append( "var backgroundData = '';" );
				buffer.Append( "var inkData = '';" );
				if( formId != null && backgroundUrl != null && inkUrl != null )
				{
					string backgroundAbsolutePath = PathUtil.getAbsolutePath( backgroundUrl );
                    if (backgroundAbsolutePath.IndexOf(ConfigUtil.ResourceFilePath()) == 0)
                    {
                        string backgroundPath = form.HttpContext.Request.MapPath(backgroundAbsolutePath);
                        FileInfo backgroundFile = new FileInfo(backgroundPath);
                        if (backgroundFile.Exists && isImageFile(backgroundFile))
                        {
                            FileStream input = new FileStream(backgroundPath, FileMode.Open, FileAccess.Read);
                            byte[] content = new byte[(int)input.Length];
                            input.Read(content, 0, content.Length);
                            input.Close();
                            string base64Content = XImageEncoder.encodeBASE64(content);
                            buffer.Append("backgroundData = '" + base64Content + "';");
                        }
                    }
					string inkAbsolutePath = PathUtil.getAbsolutePath( inkUrl );
					if( inkAbsolutePath.IndexOf( ConfigUtil.InlineFilePath() ) == 0 )
					{
						string inkPath = form.HttpContext.Request.MapPath( inkAbsolutePath );
						FileInfo inkFile = new FileInfo( inkPath );
						if( inkFile.Exists && isImageFile( inkFile ) )
						{
							FileStream input = new FileStream( inkPath, FileMode.Open, FileAccess.Read );
							byte[] content = new byte[ (int)input.Length ];
							input.Read( content, 0, content.Length );
							input.Close();
							string base64Content = XImageEncoder.encodeBASE64( content );
							buffer.Append( "inkData = '" + base64Content + "';" );
						}
					}
				}
				buffer.Append( parent + "InfoJet_SetInkPictures( backgroundData, inkData );" );
			}
			catch( Exception e )
			{
				FileLog.log( e );
                //CID 0872 //2011.04.15 //Begin
                string message = ScriptHtmlUtil.formatMessage(e.Message);
                buffer.Append(parent + "alert('" + message + "')");
                //CID 0872 //2011.04.15 //End
			}
			if( !ConfigUtil.UseAJAX() )
			{
				buffer.Append( "</script>" );
			}
			return buffer.ToString();
		}

		private static string[] ExtArray = { ".bmp", ".emf", ".exif", ".gif", ".icon", ".jpg", ".png", ".tiff", ".wmf", ".jpeg" };

		private static bool isImageFile( FileInfo file )
		{
			string ext = file.Extension.ToString();
			foreach( string imgExt in ExtArray )
			{
				if( ext.Equals( imgExt ) )
				{
					return true;
				}
			}
			return false;
		}
	}
}
//CID 0499 //2010.03.18 //End
